package com.banyuan.servlet.opus;


import com.banyuan.dao.OpusDao;
import com.banyuan.dao.TypesDao;
import com.banyuan.domain.Opus;
import com.banyuan.domain.Types;
import com.banyuan.utils.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@WebServlet("/opus/listPage")
public class ListPageServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer loginId = AuthorUtils.getLoginId(req);


        //  ---------------------模糊查询开始-----------------------------
        String name = req.getParameter("name");
        String intro = req.getParameter("intro");
        String beginTime = req.getParameter("beginTime");
        String endTime = req.getParameter("endTime");
        String typeId = req.getParameter("typeId");

        // 拼 sql语句
        // 添加objct条件


        StringBuffer condition = new StringBuffer();
        List<Object> al = new ArrayList<>();


        if (!CommonUtils.isEmpty(name)) {
            condition.append(" and opus.name like ? ");
            al.add("%"+name.trim()+"%");
        }

        if (!CommonUtils.isEmpty(intro)) {
            condition.append(" and opus.intro like ? ");
            al.add("%"+intro.trim()+"%");
        }

        if (!CommonUtils.isEmpty(typeId)) {
            condition.append(" and opus.typeId = ? ");
            al.add(typeId);
        }

        if (!CommonUtils.isEmpty(beginTime)) {
            condition.append(" and opus.createTime >= ? ");
            al.add(beginTime);
        }

        if (!CommonUtils.isEmpty(endTime)) {
            condition.append("  and opus.createTime <= ? ");
            Date date = DateUtils.StringToDate(endTime);
            long time = date.getTime() + (24*60*60*1000) - 1;
            date.setTime(time);
            al.add(date);
        }


        condition.append(" and opus.authorId = ?");
        al.add(loginId);

        //  ---------------------模糊查询结束-----------------------------


        // ---------------------  分页开始----------------------------
        String limit = PageInfoUtils.pageInfo(req, condition.toString(),al,new OpusDao());


        // ---------------------  分页结束----------------------------



        ResultSet resultSet = null;
        ResultSet rs = null;
        try {
             resultSet = OpusDao.selectAll(condition.toString(),limit,al.toArray());
            List<Opus> opusList = DBUtils.resultToBeanAll(resultSet, Opus.class);
            req.setAttribute("opusList",opusList);

            rs = TypesDao.selectAll("","");
            List<Types> typesList = DBUtils.resultToBeanAll(rs, Types.class);
            req.setAttribute("typeList",typesList);


            req.getRequestDispatcher("/WEB-INF/pages/opus/list.jsp").forward(req,resp);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeBy(resultSet);
            JDBCUtils.closeBy(rs);
        }


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
